﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using YAPL.Language.Ast;
using YAPL.Language.Ast.Transformation;
using YAPL.Language.Ast.Utils;

namespace YAPL.Test.YAPL.Language.Ast.Transformation {
	public class AstOptimizerTest {

		private readonly AstOptimizer _optimizer = new AstOptimizer();

		[Fact]
		public void ExpressionInConstantFormulaProgramShouldBeTransformedIntoConstant() {
			var expr = TestHelper.GetAstFromYAPL((FixturePath.ConstantFormula_yapl));
			var result = _optimizer.Optimize(expr);

			var walker = new DefaultAstWalker(result, AstWalkType.PreOrder);
			Expression formulaResult = null;

			while (walker.MoveNext()) {
				formulaResult = walker.Current;
			} // after that formulaResult should point to precomputed constant

			
			Assert.IsType(typeof(ConstantExpression), formulaResult);
			var constant = (ConstantExpression) formulaResult;
			Assert.IsType(typeof(string), constant.Value);
			Assert.Equal("21,2 dollars in my p0cket", (string)constant.Value);
		}
	}
}
